import { IPackNifi } from "src/inter";
import Nifi from "../../nifi";
import {Cell} from "pack-antv-x6"
import markup from "./markup";
import { DEFAULT_CELL_NAME,DEFAULT_NODE_PORTS } from "../metadata";
import { pickData } from "../../util"
import FormElem from "./form.vue"
import { h } from "vue";
import {t} from "../../i18n/index"

const KEYS: [key: string, sk?: any][] = [
    ['id'],
    ['label', 'component.name'],
    ["position.x", "position.x"],
    ["position.y", "position.y"],
    ['version', 'revision.version']
];

const name = DEFAULT_CELL_NAME.outputPorts.en;
const shape = `node-${name}`;
//
export default (nifi:Nifi):IPackNifi.MoudleOutputPorts => {
    //
    nifi.graph.registerNode(shape,markup)
    //
    const init = (tds:any[]) => {
        const res: any[] = [];
        for (const td of new Array().concat(tds)) {
            if (!td) continue;
            nifi.cells.set(td)
            const md: any = { ...pickData(td, KEYS), name, shape,status:"RUN" };
            res.push(md)
        }
        return res;
    }
    
    // 新增元素
    const add = async (d:any) => {
        console.log('+++++++++++++',d)
        //
        const td = await new Promise<any>((resolve) => {
            nifi.drawer.open(h(FormElem, { nifi,resolve,component:d }) as any)
        })

        if (td) {
            const mds = init(td);
            nifi.graph?.addNode(mds[0]);
        }
        // console.log('============',sd)
        // 弹出表单输入
        // 
        // 
        // console.log(data)
        // const td = await nifi.cells.addCell(name, data)
        // const md = init(td)[0];
        // if(md){
        //     nifi.graph?.addNode(md);
        // }
    }

    const addNode = async (component:any) => {
        const data = { component, disconnectedNodeAcknowledged: false, revision: { clientId: nifi.flow.clientId, version: 0 } }
        console.log('======',data)
        try {
            const td = await nifi.cells.addCell(name,data)
            
            return td  ;
        } catch (error) {
            console.error(error)
            return error;
        }
    }
    // 更新元素
    const set = async (data:any) => {}
    //
    const del = async (data:any) => {}
    //
    const refresh = async (node:Cell) => {}
    //
    const update = (data:any) => {}
    //
    const edit = async (cell:Cell) => {
        nifi.drawer.open();
        let vnode:any
        await refresh(cell)
        vnode = h(FormElem, { nifi, cell })
        nifi.drawer.set(vnode)
    }
    const view = (node:Cell) => {}
    //
    const contextmenu = (node:Cell,evt:MouseEvent) => {}

    return {name,init,add,addNode,set,del,refresh,update,edit,view,contextmenu}
}